home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / c / ExtrasLib.lha / ExtrasLib / Source / MAPools.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-30  |  5.5 KB  |  212 lines

  1. #include <proto/exec.h>
  2. #include <clib/extras_protos.h>
  3.  
  4.  
  5. /****** extras.lib/MultiAllocPooledA ******************************************
  6. *
  7. *   NAME
  8. *       MultiAllocPooledA -- Multiple AllocPooled.
  9. *       MultiAllocPooled -- varargs stub.
  10. *
  11. *   SYNOPSIS
  12. *       succes = MultiAllocPooledA(Flags, PoolTagList)
  13. *
  14. *       BOOL MultiAllocPooled(ULONG, struct PoolTag*);
  15. *
  16. *       succes = MultiAllocPooledA(Flags, PoolTag)
  17. *
  18. *       BOOL MultiAllocPooledA(Flags, ULONG, ...);
  19. *
  20. *   FUNCTION
  21. *       Attempt to allocate one or more memory chunks
  22. *       using AllocPooled.
  23. *
  24. *   INPUTS
  25. *       Flags - MA_FAILSIZE0: fail all allocations if any
  26. *               have a size of 0.  if your application will be
  27. *               allocating memory of dynamic sizes, and if
  28. *               you want allocations of 0 bytes to fail, then
  29. *               set this flag.
  30. *       PoolTag - pointer to an array of struct PoolTag.
  31. *                  pt_Ptr is the address of a pointer.
  32. *                  pt_Size is the size of the allocation.
  33. *                Last tag should have vt_Ptr = NULL.
  34. *                 
  35. *   RESULT
  36. *       zero if it couldn't allocate the requested memory. or non-zero
  37. *       on success. In either case, vt_Ptrs will be point to a allocated 
  38. *       memory chunk or NULL.  
  39. *
  40. *   EXAMPLE
  41. *       EX1:
  42. *         APTR pool;
  43. *         struct foo *bar;
  44. *         STRPTR dest;
  45. *         APTR cow;
  46. *
  47. *         if(pool=CreatePool(MEMF_ANY,300,300))
  48. *         {
  49. *           if( MultiAllocPooled(pool,0,
  50. *                           &bar,  sizeof(struct foo),
  51. *                           &dest, 50,       
  52. *                           &cow,  100,
  53. *                           0))
  54. *           {
  55. *             ...
  56. *             MultiFreePooled(pool,3,
  57. *                               bar,  sizeof(struct foo),
  58. *                               dest, 50,
  59. *                               cow,  100);
  60. *           }
  61. *           DeletePool(pool);
  62. *         }
  63. *
  64. *       EX2: This will never fail.
  65. *         if(MultiAllocPooled(pool,0,
  66. *                           &foo, 0,
  67. *                           0)
  68. *         {...}
  69. *
  70. *       EX3: This will always fail.
  71. *         if(MultiAllocPooled(pool,MA_FAILSIZE0,
  72. *                           &foo, 0,
  73. *                           0)
  74. *         {...}
  75. *
  76. *   NOTES
  77. *       requires exec.library to be open.
  78. *
  79. *       if the MA_FAILSIZE0 flag is not set, 0 byte allocations
  80. *       will pass even though no memory will be allocated for that.
  81. *       entry and mt_Ptr will be set to 0.
  82. *
  83. *       The memory allocated may be freed individually with 
  84. *       exec.library/FreePooled()
  85. *
  86. *   BUGS
  87. *
  88. *   SEE ALSO
  89. *       MultiFreeVecA(), MultiAllocMemA(),MultiFreeMemA(),
  90. *       MultiFreePooledA(),
  91. *       exec.library/AllocVec(), exec.library/FreeVec()
  92. *       exec.library/AllocMem(), exec.library/FreeMem()
  93. *       exec.library/AllocPooled(), exec.library/FreePooled()
  94. ******************************************************************************
  95. *
  96. */
  97.  
  98.  
  99.  
  100. BOOL MultiAllocPooled(APTR Pool, ULONG Flags, ULONG PoolTag, ... )
  101. {
  102.   return(MultiAllocPooledA(Pool, Flags,(struct PoolTag *)&PoolTag));
  103. }
  104.  
  105. BOOL MultiAllocPooledA(APTR Pool, ULONG Flags, struct PoolTag *PoolTagList)
  106. {
  107.   struct PoolTag *tag;
  108.   
  109.   if(PoolTagList)
  110.   {
  111.     tag=PoolTagList;
  112.     while(tag->pt_Ptr)
  113.     {
  114.       *tag->pt_Ptr=0;
  115.       tag++;
  116.     }
  117.  
  118.     tag=PoolTagList;
  119.     
  120.     if(Flags & MA_FAILSIZE0)
  121.     {
  122.       while(tag->pt_Ptr)
  123.       {
  124.         if(tag->pt_Size==0) return(FALSE);
  125.         tag++;
  126.       }
  127.       tag=PoolTagList;
  128.     }
  129.        
  130.     while(tag->pt_Ptr)
  131.     {
  132.       if(tag->pt_Size)
  133.       {
  134.         if(!(*tag->pt_Ptr=AllocPooled(Pool, tag->pt_Size)))
  135.         {
  136.           tag=PoolTagList;
  137.           while(tag->pt_Ptr)
  138.           {
  139.             if(tag->pt_Ptr) FreePooled(Pool, *tag->pt_Ptr, tag->pt_Size);
  140.             tag++;
  141.           }
  142.           return(FALSE);     
  143.         }
  144.       }
  145.       else
  146.         *tag->pt_Ptr=0;
  147.       
  148.       tag++;
  149.     }
  150.     return(TRUE);
  151.   }
  152.   return(FALSE);
  153. }
  154.  
  155. /****** extras.lib/MultiFreePooledA ******************************************
  156. *
  157. *   NAME
  158. *       MultiFreePooledA -- Free multiple memory chunks.
  159. *       MultiFreePooled -- varargs stub.
  160. *
  161. *   SYNOPSIS
  162. *       MultiFreePooledA(Pool, Args, FreeTagList)
  163. *
  164. *       void MultiFreePooledA(APTR, ULONG, struct FreeTag *);
  165. *
  166. *       MultiFreePooled(Pool, Args, FreeTag, ... )
  167. *
  168. *       void MultiFreePooled(APTR, ULONG, ULONG, ... );
  169. *
  170. *   FUNCTION
  171. *       Free multiple memory blocks allocated with MultiAllocPooledA()
  172. *       or exec.library/AllocPooled(). 
  173. *
  174. *   INPUTS
  175. *       Args - Number of blocks that are to be freed.
  176. *       FreeTagList - An array of FreeTags. may be NULL.
  177. *                       ft_Ptr - contains a pointer to a
  178. *                                memory block or NULL.
  179. *                       ft_Size - the size of the block.
  180. *
  181. *   RESULT
  182. *       none.
  183. *
  184. *   NOTES
  185. *       requires exec.library to be open.
  186. *
  187. *   SEE ALSO
  188. *       MultiAllocVecA(), MultiFreeVecA(), MultiAllocMemA(),
  189. *       MultiAllocPooledA(), MultiFreePooledA(),
  190. *       exec.library/AllocVec(), exec.library/FreeVec()
  191. *       exec.library/AllocMem(), exec.library/FreeMem()
  192. *       exec.library/AllocPooled(), exec.library/FreePooled()
  193. *
  194. ******************************************************************************
  195. *
  196. */
  197.  
  198. void MultiFreePooled(APTR Pool, ULONG Args, ULONG FreeTag, ... )
  199. {
  200.   MultiFreePooledA(Pool, Args,(struct FreeTag *)&FreeTag);
  201. }
  202.  
  203. void MultiFreePooledA(APTR Pool, ULONG Args, struct FreeTag *FreeTagList)
  204. {
  205.   LONG l;
  206.   
  207.   for(l=0;l<Args;l++)
  208.   {
  209.     FreePooled(Pool, FreeTagList[l].ft_Ptr, FreeTagList[l].ft_Size);
  210.   }
  211. }
  212.